﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DQYT.HTDZ.Model;
using System.Text;
using System.Data.OracleClient;
using Maticsoft.DBUtility;
using DQYT.HTDZ.Common;
using System.Data;

namespace DQYT.HTDZ.Web.Views.LSZD
{
    public partial class Add_advise : System.Web.UI.Page
    {
        string wellid;
        string jcrq;        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserName"]==null)
                {
                    Response.Redirect("../../Default.aspx");
                }


                   wellid = Server.UrlDecode(Request.QueryString["wellid"]);//Request.QueryString["wellid"];
                    jcrq = Request.QueryString["jcrq"];
                    Session["ZD_ID"] = Request.QueryString["zdid"];                    
                    if (wellid == "")
                        return;
                    //页面赋值
                    lbl_jh.Text = wellid;
                    lbl_cjrq.Text = jcrq;
                    lbl_zdr.Text = Session["UserName"].ToString();
                    if (Session["ZD_ID"] != null)
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("select ID,WELLID,ZDSJ,JCRQ,ZDJY,ZDR from bdtepzdjy");
                        strSql.Append(" where id=:id ");
                        OracleParameter[] parameters = {
                    new OracleParameter(":id", OracleType.VarChar,30) };
                        parameters[0].Value = Session["ZD_ID"].ToString();

                        DataTable dt = DbHelperOra.Query(strSql.ToString(), parameters).Tables[0];
                        lbl_jh.Text = dt.Rows[0]["wellid"].ToString().Trim();
                        lbl_cjrq.Text = dt.Rows[0]["jcrq"].ToString().Trim();
                        txt_advise.Text = dt.Rows[0]["ZDJY"].ToString();
                    }
                  
            }
          
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            BDTEPZDJYModel model = new BDTEPZDJYModel();
            model.WELLID = lbl_jh.Text.Trim();
            model.JCRQ = Convert.ToDateTime(lbl_cjrq.Text.Trim());
            if (txt_advise.Text.Trim() == "")
            {
                JsAlert.Alert("诊断建议不能为空！");
                return;
            }
            model.ZDJY = txt_advise.Text.Trim();
            model.ZDR = lbl_zdr.Text.Trim();
            model.ZDSJ = DateTime.Now;
            if (Session["ZD_ID"] == null)//添加操作
            {
                bool success = Add(model);
                if (success)
                {
                    Response.Write("<script language=javascript>alert('添加成功！');frameElement.api.close();</script>");                    
                }
                else
                {
                    JScript.Alert("添加失败！");
                }
            }
            else //修改操作
            {
                model.ID = Convert.ToDecimal(Session["ZD_ID"].ToString());
                bool success = Update(model);
                if (success)
                {
                    Response.Write("<script language=javascript>alert('修改成功！');frameElement.api.close();</script>");
                   
                }
                else
                {
                    JScript.Alert("修改失败！");
                }
            }

        }

        #region 添加和删除语句
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(BDTEPZDJYModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into bdtepzdjy(");
            strSql.Append("WELLID,JCRQ,ZDSJ,ZDJY,ZDR)");
            strSql.Append(" values (");
            strSql.Append(":WELLID,:JCRQ,:ZDSJ,:ZDJY,:ZDR)");
            OracleParameter[] parameters = {
					new OracleParameter(":WELLID", OracleType.VarChar,50),
					new OracleParameter(":JCRQ", OracleType.DateTime),
					new OracleParameter(":ZDSJ", OracleType.DateTime),
					new OracleParameter(":ZDJY", OracleType.VarChar,500),
					new OracleParameter(":ZDR", OracleType.VarChar,20)};

            parameters[0].Value = model.WELLID;
            parameters[1].Value = model.JCRQ;
            parameters[2].Value = model.ZDSJ;
            parameters[3].Value = model.ZDJY;
            parameters[4].Value = model.ZDR;


            int rows = DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BDTEPZDJYModel model)
        {
            string strJCRQ = ((DateTime)model.JCRQ).ToString("yyyy-MM-dd-HH-mm-ss");           

            string strSql = "update bdtepzdjy set zdjy='" + model.ZDJY + "', zdr='" + model.ZDR + "' ,zdsj=sysdate where ID='" + model.ID + "' ";

            int rows = DbHelperOra.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }              
           
        }
        #endregion

        protected void btn_reset_Click(object sender, EventArgs e)
        {
            txt_advise.Text = "";
        }
    }
}